﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using KerriganDataContract.Formation;
using System.ServiceModel;
using KerriganDataContract.Vehicule;

namespace KerriganService.DAC
{
    public static class FormationCategoryDAC
    {
        public static List<FormationCategoryDC> GetFormationCategories()
        {
            List<FormationCategoryDC> res = new List<FormationCategoryDC>();
            
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    List<TFormationCategory> entities = db.TFormationCategories.ToList();
                    res = CopyListEntitiesToListBos(entities);
                }   
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return res;
        }
        
        public static TFormationCategory CopyBoToEntity(FormationCategoryDC bo)
        {
            TFormationCategory entity = new TFormationCategory();

            try
            {
                entity.IdFomationCategory = bo.IdFormationCategory;
                entity.IdTypeVehicleFK = bo.TypeVehicle.IdTypeVehicle;
                entity.Name = bo.Name;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static FormationCategoryDC CopyEntityToBo(TFormationCategory entity)
        {
            FormationCategoryDC bo = new FormationCategoryDC();

            try
            {
                bo.IdFormationCategory = entity.IdFomationCategory;
                bo.Name = entity.Name;
                bo.TypeVehicle = new TypeVehicleDC();
                bo.TypeVehicle = TypeVehicleDAC.CopyEntityToBo(entity.TTypeVehicle);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        public static List<FormationCategoryDC> CopyListEntitiesToListBos(List<TFormationCategory> entities)
        {
            List<FormationCategoryDC> bo = new List<FormationCategoryDC>();

            try
            {
                foreach (TFormationCategory entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
    }
}